setwd("C:\\Users\\Nicho\\Dropbox\\2020 Campaign Stops")
load("contribution-data-20210412.RData")
stops <- read.csv("campaign_stops.csv", stringsAsFactors = F)
library(lfe)
library(stargazer)
library(sjPlot)
library(ggplot2)
library(maps)
library(usmap)
library(plyr)
library(dplyr)
library(TTR)

#### County-level models ####
mod_county_1 <- felm(log(biden_amount+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)
mod_county_2 <- felm(log(biden_count+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)
mod_county_3 <- felm(log(trump_amount+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)
mod_county_4 <- felm(log(trump_count+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)

stargazer(mod_county_1, mod_county_2, mod_county_3, mod_county_4,
          title = "County-level Results",
          label = "county-level",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                             "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("FEs", "Zip \\& Day", "Zip \\& Day",
                             "Zip \\& Day", "Zip \\& Day"),
                           c("Counties", rep(3204,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables\\county-level.tex")

#### County-level models 51-200 donations ####
mod_county_1 <- felm(log(biden_amount_51_200+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)
mod_county_2 <- felm(log(biden_count_51_200+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)
mod_county_3 <- felm(log(trump_amount_51_200+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)
mod_county_4 <- felm(log(trump_count_51_200+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)

stargazer(mod_county_1, mod_county_2, mod_county_3, mod_county_4,
          title = "County-level Results (Donations between \\$51--\\$200)",
          label = "county-level-51-200",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("Counties", rep(3204,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables//county-level-51-200.tex")

#### County-level models 201-500 donations ####
mod_county_1 <- felm(log(biden_amount_201_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)
mod_county_2 <- felm(log(biden_count_201_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)
mod_county_3 <- felm(log(trump_amount_201_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)
mod_county_4 <- felm(log(trump_count_201_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)

stargazer(mod_county_1, mod_county_2, mod_county_3, mod_county_4,
          title = "County-level Results (Donations between \\$201--\\$500)",
          label = "county-level-201-500",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("Counties", rep(3204,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables//county-level-201-500.tex")

#### County-level models under 500 donations ####
mod_county_1 <- felm(log(biden_amount_under_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)
mod_county_2 <- felm(log(biden_count_under_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)
mod_county_3 <- felm(log(trump_amount_under_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)
mod_county_4 <- felm(log(trump_count_under_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)

stargazer(mod_county_1, mod_county_2, mod_county_3, mod_county_4,
          title = "County-level Results (Donations under \\$500)",
          label = "county-level-under-500",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("Counties", rep(3204,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables//county-level-under-500.tex")

mod_county_1_battleground <- felm(log(biden_amount_under_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_county_2_battleground <- felm(log(biden_count_under_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_county_3_battleground <- felm(log(trump_amount_under_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_county_4_battleground <- felm(log(trump_count_under_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])

to_plot <- data.frame(
  coefficient = c(mod_county_1$coefficients, mod_county_2$coefficients,
                mod_county_3$coefficients, mod_county_4$coefficients,
                mod_county_1_battleground$coefficients, 
                mod_county_2_battleground$coefficients, 
                mod_county_3_battleground$coefficients, 
                mod_county_4_battleground$coefficients),
  se = c(mod_county_1$cse, mod_county_2$cse,
         mod_county_3$cse, mod_county_4$cse,
         mod_county_1_battleground$cse, 
         mod_county_2_battleground$cse, 
         mod_county_3_battleground$cse, 
         mod_county_4_battleground$cse),
  visit = rep(c("Biden Stop", "Trump Stop", "Harris Stop", "Pence Stop"), 8),
  model = rep(c("Biden Dollar Amount", "Biden Count of Donations",
                "Trump Dollar Amount", "Trump Count of Donations"), each = 4, 2),
  position = rep(c(0.066667,.2,-.2,-.066667), each = 4, 2),
  sample = rep(c("All States", "Battleground States"), each = 16)
)

to_plot$visit <- factor(to_plot$visit, levels = c("Pence Stop", "Harris Stop",
                                                  "Trump Stop", "Biden Stop"))

pdf(file = "Figures//coefplot.pdf", width = 8, height = 4)
ggplot(to_plot, aes(x = visit, y = coefficient,
                    color = model, pch = model,
                    ymin = coefficient - 1.64*se,
                    ymax = coefficient + 1.64*se)) + 
  geom_errorbar(width = 0, position = position_nudge(x = to_plot$position)) +
  geom_point(position = position_nudge(x = to_plot$position), size = 2, fill = "white") + 
  coord_flip() + geom_hline(yintercept = 0, lty = 2, size = 1) +
  scale_shape_manual(name = "Outcome", values = c(21,24,16,17)) +
  scale_color_manual(name = "Outcome", values = c("Blue", "Blue", "Red", "Red")) +
  theme_bw() + ylim(c(-.1, .25)) + xlab("") + ylab("Esimated Coefficient") +
  facet_wrap(~ sample) + theme(panel.grid.minor = element_blank())
dev.off()

biden_treated_counties <- aggregate(biden_stop_DMA ~ DMA, contrib.dat, sum)
biden_treated_counties <- subset(biden_treated_counties, biden_stop_DMA>0)
biden_amt_mean <- aggregate(biden_amount_under_500_avg ~ DMA, contrib.dat, sum)
biden_amt_mean <- mean(biden_amt_mean$biden_amount_under_500_avg[biden_amt_mean$DMA %in% biden_treated_counties$DMA])

biden_count_mean <- aggregate(biden_count_under_500_avg ~ DMA, contrib.dat, sum)
biden_count_mean <- mean(biden_count_mean$biden_count_under_500_avg[biden_count_mean$DMA %in% biden_treated_counties$DMA])

trump_treated_counties <- aggregate(trump_stop_DMA ~ DMA, contrib.dat, sum)
trump_treated_counties <- subset(trump_treated_counties, trump_stop_DMA>0)
trump_amt_mean <- aggregate(trump_amount_under_500_avg ~ DMA, contrib.dat, sum)
trump_amt_mean <- mean(trump_amt_mean$trump_amount_under_500_avg[trump_amt_mean$DMA %in% trump_treated_counties$DMA])

trump_count_mean <- aggregate(trump_count_under_500_avg ~ DMA, contrib.dat, sum)
trump_count_mean <- mean(trump_count_mean$trump_count_under_500_avg[trump_count_mean$DMA %in% trump_treated_counties$DMA])


means <- data.frame(visit = rep(c("Biden Stop", "Trump Stop",
                                  "Harris Stop", "Pence Stop"), 8),
  treatment_mean = rep(c(biden_amt_mean, biden_count_mean,
                       trump_amt_mean, trump_count_mean), each = 4, 2))
                       

to_plot$treatment_mean <- means$treatment_mean/69
to_plot$control_mean <- to_plot$treatment_mean -
  to_plot$treatment_mean*(exp(to_plot$coefficient)-1)
to_plot$change <- to_plot$treatment_mean - to_plot$control_mean


#### DMA-level models ####
mod_DMA_1 <- felm(log(biden_amount+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, data = contrib.dat)
mod_DMA_2 <- felm(log(biden_count+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, data = contrib.dat)
mod_DMA_3 <- felm(log(trump_amount+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, data = contrib.dat)
mod_DMA_4 <- felm(log(trump_count+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, data = contrib.dat)

stargazer(mod_DMA_1, mod_DMA_2, mod_DMA_3, mod_DMA_4,
          title = "DMA-level Results",
          label = "dma-level",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("FEs", "Zip \\& Day", "Zip \\& Day",
                             "Zip \\& Day", "Zip \\& Day"),
                           c("DMAs", rep(210,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables\\dma-level.tex")

#### DMA-level models 51-200 donations ####
mod_DMA_1 <- felm(log(biden_amount_51_200+1) ~
                       biden_stop_DMA + trump_stop_DMA +
                       harris_stop_DMA + pence_stop_DMA |
                       zip + day | 0 | DMA, data = contrib.dat)
mod_DMA_2 <- felm(log(biden_count_51_200+1) ~
                       biden_stop_DMA + trump_stop_DMA +
                       harris_stop_DMA + pence_stop_DMA |
                       zip + day | 0 | DMA, data = contrib.dat)
mod_DMA_3 <- felm(log(trump_amount_51_200+1) ~
                       biden_stop_DMA + trump_stop_DMA +
                       harris_stop_DMA + pence_stop_DMA |
                       zip + day | 0 | DMA, data = contrib.dat)
mod_DMA_4 <- felm(log(trump_count_51_200+1) ~
                       biden_stop_DMA + trump_stop_DMA +
                       harris_stop_DMA + pence_stop_DMA |
                       zip + day | 0 | DMA, data = contrib.dat)

stargazer(mod_DMA_1, mod_DMA_2, mod_DMA_3, mod_DMA_4,
          title = "DMA-level Results (Donations between \\$51--\\$200)",
          label = "dma-level-51-200",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("FEs", "Zip \\& Day", "Zip \\& Day",
                             "Zip \\& Day", "Zip \\& Day"),
                           c("DMAs", rep(210,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables\\dma-level-51-200.tex")

#### DMA-level models 201-500 donations ####
mod_DMA_1 <- felm(log(biden_amount_201_500+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, data = contrib.dat)
mod_DMA_2 <- felm(log(biden_count_201_500+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, data = contrib.dat)
mod_DMA_3 <- felm(log(trump_amount_201_500+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, data = contrib.dat)
mod_DMA_4 <- felm(log(trump_count_201_500+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, data = contrib.dat)

stargazer(mod_DMA_1, mod_DMA_2, mod_DMA_3, mod_DMA_4,
          title = "DMA-level Results (Donations between \\$201--\\$500)",
          label = "dma-level-201-500",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("FEs", "Zip \\& Day", "Zip \\& Day",
                             "Zip \\& Day", "Zip \\& Day"),
                           c("DMAs", rep(210,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables\\dma-level-201-500.tex")


#### DMA-level models under 500 donations ####
mod_DMA_1 <- felm(log(biden_amount_under_500+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, data = contrib.dat)
mod_DMA_2 <- felm(log(biden_count_under_500+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, data = contrib.dat)
mod_DMA_3 <- felm(log(trump_amount_under_500+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, data = contrib.dat)
mod_DMA_4 <- felm(log(trump_count_under_500+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, data = contrib.dat)

stargazer(mod_DMA_1, mod_DMA_2, mod_DMA_3, mod_DMA_4,
          title = "DMA-level Results (Donations under \\$500)",
          label = "dma-level-under-500",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("FEs", "Zip \\& Day", "Zip \\& Day",
                             "Zip \\& Day", "Zip \\& Day"),
                           c("DMAs", rep(210,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables\\dma-level-under-500.tex")








#### County-level models, battleground states ####
mod_county_1 <- felm(log(biden_amount+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county,
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_county_2 <- felm(log(biden_count+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])

mod_county_3 <- felm(log(trump_amount+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_county_4 <- felm(log(trump_count+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])

stargazer(mod_county_1, mod_county_2, mod_county_3, mod_county_4,
          title = "County-level Results, Battleground States",
          label = "county-level-battleground",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("FEs", "Zip \\& Day", "Zip \\& Day",
                             "Zip \\& Day", "Zip \\& Day"),
                           c("Counties", rep(1192,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables\\county-level-battleground.tex")

#### County-level models 51-200 donations, battleground states ####
mod_county_1 <- felm(log(biden_amount_51_200+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_county_2 <- felm(log(biden_count_51_200+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_county_3 <- felm(log(trump_amount_51_200+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county,
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_county_4 <- felm(log(trump_count_51_200+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])

stargazer(mod_county_1, mod_county_2, mod_county_3, mod_county_4,
          title = "County-level Results, Battleground States (Donations between \\$51--\\$200)",
          label = "county-level-51-200-battleground",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("Counties", rep(1192,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables//county-level-51-200-battleground.tex")

#### County-level models 201-500 donations, battleground states ####
mod_county_1 <- felm(log(biden_amount_201_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county,
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_county_2 <- felm(log(biden_count_201_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_county_3 <- felm(log(trump_amount_201_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_county_4 <- felm(log(trump_count_201_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])

stargazer(mod_county_1, mod_county_2, mod_county_3, mod_county_4,
          title = "County-level Results, Battleground States (Donations between \\$201--\\$500)",
          label = "county-level-201-500-battleground",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("Counties", rep(1192,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables//county-level-201-500-battleground.tex")

#### County-level models under 500 donations, battleground states ####
mod_county_1 <- felm(log(biden_amount_under_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_county_2 <- felm(log(biden_count_under_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_county_3 <- felm(log(trump_amount_under_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_county_4 <- felm(log(trump_count_under_500+1) ~
                       biden_stop_county + trump_stop_county +
                       harris_stop_county + pence_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
stargazer(mod_county_1, mod_county_2, mod_county_3, mod_county_4,
          title = "County-level Results, Battleground States (Donations under \\$500)",
          label = "county-level-under-500-battleground",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("Counties", rep(1192,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables//county-level-under-500-battleground.tex")






#### DMA-level models, battleground states ####
mod_DMA_1 <- felm(log(biden_amount+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, 
                  data = contrib.dat[contrib.dat$state.abb %in%
                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                         "PA", "NH"),])

mod_DMA_2 <- felm(log(biden_count+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA,
                  data = contrib.dat[contrib.dat$state.abb %in%
                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                         "PA", "NH"),])

mod_DMA_3 <- felm(log(trump_amount+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA,
                  data = contrib.dat[contrib.dat$state.abb %in%
                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                         "PA", "NH"),])

mod_DMA_4 <- felm(log(trump_count+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, 
                  data = contrib.dat[contrib.dat$state.abb %in%
                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                         "PA", "NH"),])

stargazer(mod_DMA_1, mod_DMA_2, mod_DMA_3, mod_DMA_4,
          title = "DMA-level Results, Battleground States",
          label = "dma-level-battleground",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("FEs", "Zip \\& Day", "Zip \\& Day",
                             "Zip \\& Day", "Zip \\& Day"),
                           c("DMAs", rep(107,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables\\dma-level-battleground.tex")

#### DMA-level models 51-200 donations, battleground states ####
mod_DMA_1 <- felm(log(biden_amount_51_200+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, 
                  data = contrib.dat[contrib.dat$state.abb %in%
                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                         "PA", "NH"),])
mod_DMA_2 <- felm(log(biden_count_51_200+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA,
                  data = contrib.dat[contrib.dat$state.abb %in%
                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                         "PA", "NH"),])
mod_DMA_3 <- felm(log(trump_amount_51_200+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA,
                  data = contrib.dat[contrib.dat$state.abb %in%
                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                         "PA", "NH"),])
mod_DMA_4 <- felm(log(trump_count_51_200+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, 
                  data = contrib.dat[contrib.dat$state.abb %in%
                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                         "PA", "NH"),])

stargazer(mod_DMA_1, mod_DMA_2, mod_DMA_3, mod_DMA_4,
          title = "DMA-level Results, Battleground States (Donations between \\$51--\\$200)",
          label = "dma-level-51-200-battleground",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("FEs", "Zip \\& Day", "Zip \\& Day",
                             "Zip \\& Day", "Zip \\& Day"),
                           c("DMAs", rep(107,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables\\dma-level-51-200-battleground.tex")

#### DMA-level models 201-500 donations, battleground states ####
mod_DMA_1 <- felm(log(biden_amount_201_500+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, 
                  data = contrib.dat[contrib.dat$state.abb %in%
                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                         "PA", "NH"),])
mod_DMA_2 <- felm(log(biden_count_201_500+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA,
                    data = contrib.dat[contrib.dat$state.abb %in%
                                         c("NV", "AZ", "CO", "TX", "MN", "IA",
                                           "WI", "MI", "OH", "NC", "GA", "FL",
                                           "PA", "NH"),])
mod_DMA_3 <- felm(log(trump_amount_201_500+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, 
                    data = contrib.dat[contrib.dat$state.abb %in%
                                         c("NV", "AZ", "CO", "TX", "MN", "IA",
                                           "WI", "MI", "OH", "NC", "GA", "FL",
                                           "PA", "NH"),])
mod_DMA_4 <- felm(log(trump_count_201_500+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA,
                    data = contrib.dat[contrib.dat$state.abb %in%
                                         c("NV", "AZ", "CO", "TX", "MN", "IA",
                                           "WI", "MI", "OH", "NC", "GA", "FL",
                                           "PA", "NH"),])

stargazer(mod_DMA_1, mod_DMA_2, mod_DMA_3, mod_DMA_4,
          title = "DMA-level Results, Battleground States (Donations between \\$201--\\$500)",
          label = "dma-level-201-500-battleground",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("FEs", "Zip \\& Day", "Zip \\& Day",
                             "Zip \\& Day", "Zip \\& Day"),
                           c("DMAs", rep(107,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables\\dma-level-201-500-battleground.tex")


#### DMA-level models under 500 donations, battleground states ####
mod_DMA_1 <- felm(log(biden_amount_under_500+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, 
                  data = contrib.dat[contrib.dat$state.abb %in%
                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                         "PA", "NH"),])
mod_DMA_2 <- felm(log(biden_count_under_500+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, 
                  data = contrib.dat[contrib.dat$state.abb %in%
                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                         "PA", "NH"),])
mod_DMA_3 <- felm(log(trump_amount_under_500+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, 
                  data = contrib.dat[contrib.dat$state.abb %in%
                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                         "PA", "NH"),])
mod_DMA_4 <- felm(log(trump_count_under_500+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, 
                  data = contrib.dat[contrib.dat$state.abb %in%
                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                         "PA", "NH"),])

stargazer(mod_DMA_1, mod_DMA_2, mod_DMA_3, mod_DMA_4,
          title = "DMA-level Results, Battleground States (Donations under \\$500)",
          label = "dma-level-under-500-battleground",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("FEs", "Zip \\& Day", "Zip \\& Day",
                             "Zip \\& Day", "Zip \\& Day"),
                           c("DMAs", rep(107,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables\\dma-level-under-500-battleground.tex")











#### Temperature placebos
within_dayzip_sd <- sd(felm(temperature ~ 0 | zip + day | 0 | 0,
                            data = contrib.dat)$residuals)

mod_county <- felm(I(temperature/within_dayzip_sd) ~ 
              biden_stop_county + trump_stop_county +
              harris_stop_county + pence_stop_county |
              zip + day | 0 | state_county, data = contrib.dat)

mod_DMA <- felm(I(temperature/within_dayzip_sd) ~ 
                     biden_stop_DMA + trump_stop_DMA +
                     harris_stop_DMA + pence_stop_DMA |
                     zip + day | 0 | DMA , data = contrib.dat)

stargazer(mod_county, mod_DMA,
          title = "Placebo Test, Temperature",
          label = "temp-pla",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Temperature (scaled by standard deviation)"),
          multicolumn = T,
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("FEs", "Zip \\& Day", "Zip \\& Day",
                             "Zip \\& Day", "Zip \\& Day")),
          out = "Tables\\temp-placebos.tex")


#### County-level Placebo, all states under $500 ####
mod_county_1 <- felm(log(biden_amount_under_500+1) ~
                       beyonce_stop_county + dylan_stop_county +
                       underwood_stop_county + bryan_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)
mod_county_2 <- felm(log(biden_count_under_500+1) ~
                       beyonce_stop_county + dylan_stop_county +
                       underwood_stop_county + bryan_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)
mod_county_3 <- felm(log(trump_amount_under_500+1) ~
                       beyonce_stop_county + dylan_stop_county +
                       underwood_stop_county + bryan_stop_county |
                       zip + day | 0 | state_county, data = contrib.dat)
mod_county_4 <- felm(log(trump_count_under_500+1) ~
                       beyonce_stop_county + dylan_stop_county +
                       underwood_stop_county + bryan_stop_county|
                       zip + day | 0 | state_county, data = contrib.dat)

stargazer(mod_county_1, mod_county_2, mod_county_3, mod_county_4,
          title = "County-level Results (Donations under \\$500)",
          label = "county-level-under-500",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Beyonce Stop", "Dylan Stop",
                               "Underwood Stop", "Bryan Stop"),
          add.lines = list(c("Counties", rep(3204,4)),
                           c("Days", rep(69,4))),
          out = "Tables//county-level-under-500-concert.tex")

#### County-level Placebo, battleground states under $500 ####
mod_county_1 <- felm(log(biden_amount_under_500+1) ~
                       beyonce_stop_county + dylan_stop_county +
                       underwood_stop_county + bryan_stop_county |
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_county_2 <- felm(log(biden_count_under_500+1) ~
                       beyonce_stop_county + dylan_stop_county +
                       underwood_stop_county + bryan_stop_county|
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_county_3 <- felm(log(trump_amount_under_500+1) ~
                       beyonce_stop_county + dylan_stop_county +
                       underwood_stop_county + bryan_stop_county|
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_county_4 <- felm(log(trump_count_under_500+1) ~
                       beyonce_stop_county + dylan_stop_county +
                       underwood_stop_county + bryan_stop_county|
                       zip + day | 0 | state_county, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
stargazer(mod_county_1, mod_county_2, mod_county_3, mod_county_4,
          title = "County-level Results, Battleground States (Donations under \\$500)",
          label = "county-level-under-500-battleground",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Beyonce Stop", "Dylan Stop",
                               "Underwood Stop", "Bryan Stop"),
          add.lines = list(c("Counties", rep(1192,4)),
                           c("Days", rep(69,4))),
          out = "Tables//county-level-under-500-battleground-concert.tex")


#### Stop tables ####

# All stops
table(stops$candidate)

# Battleground stops
table(stops$candidate[stops$state.abb %in%
                        c("NV", "AZ", "CO", "TX", "MN", "IA",
                          "WI", "MI", "OH", "PA", "NH")])
      
#### Map of stops ####
stop_counts <- data.frame(table(stops$state.abb, stops$candidate))
names(stop_counts) <- c("state.abb", "candidate", "stops")

holder <- data.frame(state.abb = rep(state.abb, 4),
                     candidate = rep(c("Donald Trump", "Joe Biden",
                                       "Mike Pence", "Kamala Harris"), each = 50),
                     stringsAsFactors = F)
stop_counts <- join(holder, stop_counts, type = "left")
stop_counts[is.na(stop_counts)] <- 0

names(stop_counts)[1] <- "state"

bmp(file = "Figures/stops_map.bmp", width = 8, height = 12, units = "in",
    res = 400)
plot_usmap(data = stop_counts, values = "stops") +
  scale_fill_gradient(name = "Count of Stops",
                      low = "white", high = "black") +
  facet_wrap(~candidate, drop = TRUE, ncol = 2) +
  theme(legend.position = "bottom")
dev.off()


#### Aggregate donations ####
pdf(file = "Figures//agg_amount.pdf", width = 8 , height = 5)
ggplot(contrib.dat, aes(x = day)) +
  stat_summary(geom = "line", fun = "sum",
               aes(y = biden_amount, lty = "Biden",
                   col = "Biden"),
               size = 1) +
  stat_summary(geom = "line", fun = "sum",
               aes(y = trump_amount, lty = "Trump",
                   col = "Trump"),
               size = 1) +
  stat_summary(geom = "point", fun = "sum",
               aes(y = biden_amount, pch = "Biden",
                   col = "Biden"),
               size = 2) +
  stat_summary(geom = "point", fun = "sum",
               aes(y = trump_amount, pch = "Trump",
                   col = "Trump"),
               size = 2) +
  scale_y_continuous(limits = c(0, 1.5e+07),
                     breaks = seq(0, 1.5e+07, 5e+06),
                     labels = paste("$", seq(0, 15, 5), sep = "")) +
  ylab("") + xlab("") + ggtitle("Dollars Contributed (in millions)") +
  scale_linetype_manual(name = "", values = c(1,6)) +
  scale_color_manual(name = "", values = c("Blue", "Red"),
                     labels = c("Biden", "Trump")) +
  scale_shape_manual(name = "", values = c(15,16),
                     labels = c("Biden", "Trump")) +
  theme(legend.position = "bottom") + theme_minimal() +
  annotate(geom = "text", x = as.Date("2020-09-29"),
           y = 1.5e+07, label = "1st Debate") +
  annotate(geom = "text", x = as.Date("2020-10-22"),
           y = 1.1e+07, label = "2nd Debate") +
  annotate(geom = "text", x = as.Date("2020-10-15"),
           y = 1.5e+07, label = "Town Halls") +
  annotate(geom = "text", x = as.Date("2020-10-07"),
           y = 1e+07, label = "VP Debate") +
  theme(panel.grid.minor = element_blank(),
        text = element_text(size = 12),
        legend.position = "bottom")
dev.off()

pdf(file = "Figures//agg_count.pdf", width = 8 , height = 5)
ggplot(contrib.dat, aes(x = day)) +
  stat_summary(geom = "line", fun = "sum",
               aes(y = biden_count, lty = "Biden",
                   col = "Biden"),
               size = 1) +
  stat_summary(geom = "line", fun = "sum",
               aes(y = trump_count, lty = "Trump",
                   col = "Trump"),
               size = 1) +
  stat_summary(geom = "point", fun = "sum",
               aes(y = biden_count, pch = "Biden",
                   col = "Biden"),
               size = 2) +
  stat_summary(geom = "point", fun = "sum",
               aes(y = trump_count, pch = "Trump",
                   col = "Trump"),
               size = 2) +
  scale_y_continuous(limits = c(0, 210000),
                     breaks = seq(0, 200000, 50000),
                     labels = seq(0, 200, 50)) +
  ylab("") + xlab("") + ggtitle("Individual Contributors (in thousands)") +
  scale_linetype_manual(name = "", values = c(1,2)) +
  scale_color_manual(name = "", values = c("Blue", "Red"),
                     labels = c("Biden", "Trump")) +
  scale_shape_manual(name = "", values = c(15,16),
                     labels = c("Biden", "Trump")) +
  theme(legend.position = "bottom") + theme_minimal() +
  annotate(geom = "text", x = as.Date("2020-09-29"),
           y = 130000, label = "1st Debate") +
  annotate(geom = "text", x = as.Date("2020-10-22"),
           y = 180000, label = "2nd Debate") +
  annotate(geom = "text", x = as.Date("2020-10-15"),
           y = 200000, label = "Town Halls") +
  annotate(geom = "text", x = as.Date("2020-10-07"),
           y = 120000, label = "VP Debate") +
  theme(panel.grid.minor = element_blank(),
        text = element_text(size = 12),
        legend.position = "bottom")
dev.off()


#### Permutation Placebos ####
to_draw <- data.frame(table(stops$candidate[
  stops$state.abb %in% c("NV", "AZ", "CO", "TX", "MN", "IA",
    "WI", "MI", "OH", "PA", "NH")
]), stringsAsFactors = F)

DMA_battlegrounds <- as.list(rep(NA, 250))
x <- contrib.dat
x$DMA_zip_day <- as.character(paste(x$DMA, x$zip, x$day))
for(i in 1:250){
  trump_DMA_days <- sample(unique(x$DMA_zip_day[
    x$state.abb %in% c("NV", "AZ", "CO", "TX", "MN", "IA",
                             "WI", "MI", "OH", "PA", "NH")
  ]), size = to_draw$Freq[1])
  biden_DMA_days <-  sample(unique(x$DMA_zip_day[
    x$state.abb %in% c("NV", "AZ", "CO", "TX", "MN", "IA",
                       "WI", "MI", "OH", "PA", "NH")
  ]), size = to_draw$Freq[2])
  harris_DMA_days <-  sample(unique(x$DMA_zip_day[
    x$state.abb %in% c("NV", "AZ", "CO", "TX", "MN", "IA",
                       "WI", "MI", "OH", "PA", "NH")
  ]), size = to_draw$Freq[3])
  pence_DMA_days <-  sample(unique(x$DMA_zip_day[
    x$state.abb %in% c("NV", "AZ", "CO", "TX", "MN", "IA",
                       "WI", "MI", "OH", "PA", "NH")
  ]), size = to_draw$Freq[4])
  x$trump_stop_DMA <- ifelse(x$DMA_zip_day %in% trump_DMA_days, 1, 0)
  x$biden_stop_DMA <- ifelse(x$DMA_zip_day %in% biden_DMA_days, 1, 0)
  x$harris_stop_DMA <- ifelse(x$DMA_zip_day %in% harris_DMA_days, 1, 0)
  x$pence_stop_DMA <- ifelse(x$DMA_zip_day %in% pence_DMA_days, 1, 0)
  x$DMA_state <- paste(x$DMA, x$state.abb)
  mod_DMA_1_battleground <- felm(log(biden_amount_under_500+1) ~
                                      biden_stop_DMA + trump_stop_DMA +
                                      harris_stop_DMA + pence_stop_DMA |
                                      zip + day | 0 | DMA, 
                                    data = x[x$state.abb %in%
                                                         c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                           "WI", "MI", "OH", "NC", "GA", "FL",
                                                           "PA", "NH"),])
  mod_DMA_2_battleground <- felm(log(biden_count_under_500+1) ~
                                      biden_stop_DMA + trump_stop_DMA +
                                      harris_stop_DMA + pence_stop_DMA |
                                      zip + day | 0 | DMA, 
                                    data = x[x$state.abb %in%
                                                         c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                           "WI", "MI", "OH", "NC", "GA", "FL",
                                                           "PA", "NH"),])
  mod_DMA_3_battleground <- felm(log(trump_amount_under_500+1) ~
                                      biden_stop_DMA + trump_stop_DMA +
                                      harris_stop_DMA + pence_stop_DMA |
                                      zip + day | 0 | DMA, 
                                    data = x[x$state.abb %in%
                                                         c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                           "WI", "MI", "OH", "NC", "GA", "FL",
                                                           "PA", "NH"),])
  mod_DMA_4_battleground <- felm(log(trump_count_under_500+1) ~
                                      biden_stop_DMA + trump_stop_DMA +
                                      harris_stop_DMA + pence_stop_DMA |
                                      zip + day | 0 | DMA, 
                                    data = x[x$state.abb %in%
                                                         c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                           "WI", "MI", "OH", "NC", "GA", "FL",
                                                           "PA", "NH"),])
  DMA_battlegrounds[[i]] <- data.frame(
    stop = c("Biden", "Trump", "Harris", "Pence"),
    biden_amount = c(mod_DMA_1_battleground$ctval[1],
                      mod_DMA_1_battleground$ctval[2],
                      mod_DMA_1_battleground$ctval[3],
                      mod_DMA_1_battleground$ctval[4]),
    biden_count = c(mod_DMA_2_battleground$ctval[1],
                    mod_DMA_2_battleground$ctval[2],
                    mod_DMA_2_battleground$ctval[3],
                    mod_DMA_2_battleground$ctval[4]),
    trump_amount = c(mod_DMA_3_battleground$ctval[1],
                     mod_DMA_3_battleground$ctval[2],
                     mod_DMA_3_battleground$ctval[3],
                     mod_DMA_3_battleground$ctval[4]),
    trump_count = c(mod_DMA_4_battleground$ctval[1],
                    mod_DMA_4_battleground$ctval[2],
                    mod_DMA_4_battleground$ctval[3],
                    mod_DMA_4_battleground$ctval[4]),
    iteration = i
  )
  if(i %% 10 == 0){print(i)}
}
DMA_battlegrounds <- bind_rows(DMA_battlegrounds)
save(DMA_battlegrounds, file = "battleground_permutations-DMA.RData")

g1 <- ggplot(DMA_battlegrounds, aes(x = biden_amount)) + 
  geom_density(alpha = .25, fill = "blue", color = "white") +
  facet_wrap(~stop, nrow = 2) + theme_minimal() +
  ggtitle("Placebo Distribution on Biden Dollar Amount") +
  geom_vline(data = DMA_battlegrounds[DMA_battlegrounds$stop=="Biden",],
             aes(xintercept = -0.025/0.045), lty = 2, size = 1, color = "red") +
  geom_vline(data = DMA_battlegrounds[DMA_battlegrounds$stop=="Harris",],
             aes(xintercept = 0.001/0.035), lty = 2, size = 1, color = "red") +
  geom_vline(data = DMA_battlegrounds[DMA_battlegrounds$stop=="Pence",],
             aes(xintercept = 0.025/0.021), lty = 2, size = 1, color = "red") +
  geom_vline(data = DMA_battlegrounds[DMA_battlegrounds$stop=="Trump",],
             aes(xintercept = 0.048/0.022), lty = 2, size = 1, color = "red") +
  xlab("T-Statistic") + ylab("Density")

g2 <- ggplot(DMA_battlegrounds, aes(x = biden_count)) + 
  geom_density(alpha = .25, fill = "blue", color = "white") +
  facet_wrap(~stop, nrow = 2) + theme_minimal() +
  ggtitle("Placebo Distribution on Biden Count of Donations") +
  geom_vline(data = DMA_battlegrounds[DMA_battlegrounds$stop=="Biden",],
             aes(xintercept = 0.015/0.010), lty = 2, size = 1, color = "red") +
  geom_vline(data = DMA_battlegrounds[DMA_battlegrounds$stop=="Harris",],
             aes(xintercept = 0.029/0.007), lty = 2, size = 1, color = "red") +
  geom_vline(data = DMA_battlegrounds[DMA_battlegrounds$stop=="Pence",],
             aes(xintercept = 0.002/0.005), lty = 2, size = 1, color = "red") +
  geom_vline(data = DMA_battlegrounds[DMA_battlegrounds$stop=="Trump",],
             aes(xintercept = 0.013/0.009), lty = 2, size = 1, color = "red") +
  xlab("T-Statistic") + ylab("Density")

g3 <- ggplot(DMA_battlegrounds, aes(x = trump_amount)) + 
  geom_density(alpha = .25, fill = "blue", color = "white") +
  facet_wrap(~stop, nrow = 2) + theme_minimal() +
  ggtitle("Placebo Distribution on Trump Dollar Amount") +
  geom_vline(data = DMA_battlegrounds[DMA_battlegrounds$stop=="Biden",],
             aes(xintercept = -0.005/0.028), lty = 2, size = 1, color = "red") +
  geom_vline(data = DMA_battlegrounds[DMA_battlegrounds$stop=="Harris",],
             aes(xintercept = 0.016/0.031), lty = 2, size = 1, color = "red") +
  geom_vline(data = DMA_battlegrounds[DMA_battlegrounds$stop=="Pence",],
             aes(xintercept =-0.020/0.022), lty = 2, size = 1, color = "red") +
  geom_vline(data = DMA_battlegrounds[DMA_battlegrounds$stop=="Trump",],
             aes(xintercept = 0.041/0.023), lty = 2, size = 1, color = "red") +
  xlab("T-Statistic") + ylab("Density")

g4 <- ggplot(DMA_battlegrounds, aes(x = trump_count)) + 
  geom_density(alpha = .25, fill = "blue", color = "white") +
  facet_wrap(~stop, nrow = 2) + theme_minimal() +
  ggtitle("Placebo Distribution on Trump Count of Donations") +
  geom_vline(data = DMA_battlegrounds[DMA_battlegrounds$stop=="Biden",],
             aes(xintercept = 0.005/0.005), lty = 2, size = 1, color = "red") +
  geom_vline(data = DMA_battlegrounds[DMA_battlegrounds$stop=="Harris",],
             aes(xintercept = 0.029/0.009), lty = 2, size = 1, color = "red") +
  geom_vline(data = DMA_battlegrounds[DMA_battlegrounds$stop=="Pence",],
             aes(xintercept = -0.004/0.004), lty = 2, size = 1, color = "red") +
  geom_vline(data = DMA_battlegrounds[DMA_battlegrounds$stop=="Trump",],
             aes(xintercept = 0.007/0.005), lty = 2, size = 1, color = "red") +
  xlab("T-Statistic") + ylab("Density")

pdf(file = "Figures//Placebos-DMA.pdf", width = 10, height = 8)
grid.arrange(g1, g2, g3, g4)
dev.off()  


#### Lag-Lead Placebos ####
county_lag_leads <- as.list(rep(NA, 2))
days <- c(-10,10)
for(i in 1:2){
  if(days[i]<0){
  county_lag_leads[[i]] <- contrib.dat %>%                            # Add lagged column
    group_by(zip) %>%
    dplyr::mutate(biden_amount_under_500 = dplyr::lag(biden_amount_under_500,
                                    n = -days[i], default = NA)) %>% 
    as.data.frame()
  county_lag_leads[[i]] <- county_lag_leads[[i]] %>%                            # Add lagged column
    group_by(zip) %>%
    dplyr::mutate(biden_count_under_500 = dplyr::lag(biden_count_under_500,
                                                      n = -days[i], default = NA)) %>% 
    as.data.frame()
  county_lag_leads[[i]] <- county_lag_leads[[i]] %>%                            # Add lagged column
    group_by(zip) %>%
    dplyr::mutate(trump_amount_under_500 = dplyr::lag(trump_amount_under_500,
                                                      n = -days[i], default = NA)) %>% 
    as.data.frame()
  county_lag_leads[[i]] <- county_lag_leads[[i]] %>%                            # Add lagged column
    group_by(zip) %>%
    dplyr::mutate(trump_count_under_500 = dplyr::lag(trump_count_under_500,
                                                      n = -days[i], default = NA)) %>% 
    as.data.frame()
  }
  if(days[i]>0){
    county_lag_leads[[i]] <- contrib.dat %>%                            # Add lagged column
      group_by(zip) %>%
      dplyr::mutate(biden_amount_under_500 = dplyr::lead(biden_amount_under_500,
                                                        n = days[i], default = NA)) %>% 
      as.data.frame()
    county_lag_leads[[i]] <- county_lag_leads[[i]] %>%                            # Add lagged column
      group_by(zip) %>%
      dplyr::mutate(biden_count_under_500 = dplyr::lead(biden_count_under_500,
                                                       n = days[i], default = NA)) %>% 
      as.data.frame()
    county_lag_leads[[i]] <- county_lag_leads[[i]] %>%                            # Add lagged column
      group_by(zip) %>%
      dplyr::mutate(trump_amount_under_500 = dplyr::lead(trump_amount_under_500,
                                                        n = days[i], default = NA)) %>% 
      as.data.frame()
    county_lag_leads[[i]] <- county_lag_leads[[i]] %>%                            # Add lagged column
      group_by(zip) %>%
      dplyr::mutate(trump_count_under_500 = dplyr::lead(trump_count_under_500,
                                                       n = days[i], default = NA)) %>% 
      as.data.frame()
  }
  print(i)
}

rm(contrib.dat)

lag_mods_biden_amount <- as.list(rep(NA, 2))
lag_mods_biden_count <- as.list(rep(NA, 2))
lag_mods_trump_amount <- as.list(rep(NA, 2))
lag_mods_trump_count <- as.list(rep(NA, 2))
for(i in 1:2){
  lag_mods_biden_amount[[i]] <- felm(log(biden_amount_under_500+1) ~
                                       biden_stop_county + trump_stop_county +
                                       harris_stop_county + pence_stop_county |
                                       zip + day | 0 | state_county, 
                                     data = county_lag_leads[[i]][county_lag_leads[[i]]$state.abb %in%
                                                                    c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                                      "WI", "MI", "OH", "NC", "GA", "FL",
                                                                      "PA", "NH"),])
  lag_mods_biden_count[[i]] <- felm(log(biden_count_under_500+1) ~
                                      biden_stop_county + trump_stop_county +
                                      harris_stop_county + pence_stop_county |
                                      zip + day | 0 | state_county, 
                                    data = county_lag_leads[[i]][county_lag_leads[[i]]$state.abb %in%
                                                                   c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                                     "WI", "MI", "OH", "NC", "GA", "FL",
                                                                     "PA", "NH"),])
  lag_mods_trump_amount[[i]] <- felm(log(trump_amount_under_500+1) ~
                                       biden_stop_county + trump_stop_county +
                                       harris_stop_county + pence_stop_county |
                                       zip + day | 0 | state_county, 
                                     data = county_lag_leads[[i]][county_lag_leads[[i]]$state.abb %in%
                                                                    c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                                      "WI", "MI", "OH", "NC", "GA", "FL",
                                                                      "PA", "NH"),])
  lag_mods_trump_count[[i]] <- felm(log(trump_count_under_500+1) ~
                                      biden_stop_county + trump_stop_county +
                                      harris_stop_county + pence_stop_county |
                                      zip + day | 0 | state_county, 
                                    data = county_lag_leads[[i]][county_lag_leads[[i]]$state.abb %in%
                                                                   c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                                     "WI", "MI", "OH", "NC", "GA", "FL",
                                                                     "PA", "NH"),])
  print(i)
}

coefs <- unlist(lapply(lag_mods_biden_amount, coef))
ses <- as.list(rep(NA, 2))
for(i in 1:2){
  ses[[i]] <- lag_mods_biden_amount[[i]]$cse
}
ses <- unlist(ses)

to_plot <- data.frame(
  day = rep(c(-1,0,1), each = 4),
  stop = rep(c("Biden", "Trump", "Harris", "Pence"),3),
  biden_amount = c(coefs[1:4], .085, .064, .127, .034, coefs[5:8]),
  biden_se = c(ses[1:4], 0.098, 0.050, 0.044, 0.046, ses[5:8])
)

g1 <- ggplot(to_plot, aes(x = day, y = biden_amount,
                    ymin = biden_amount - 1.64*biden_se,
                    ymax = biden_amount + 1.64*biden_se)) + 
  geom_point() + facet_wrap(~stop) + 
  geom_hline(yintercept = 0, lty = 2, col = "red") + 
  geom_errorbar(width = 0) +
  ggtitle("Effect on Dollars Donated to Biden a Week Before and After Visit") +
  theme_minimal() + scale_x_continuous(breaks = c(-2,-1,0,1, 2),
                                       labels = c("",
                                                  "One Week Before",
                                                  "Actual Visit",
                                                  "One Week After",
                                                  ""),
                                       limits = c(-2,2)) +
  xlab("") + ylab("")

coefs <- unlist(lapply(lag_mods_biden_count, coef))
ses <- as.list(rep(NA, 2))
for(i in 1:2){
  ses[[i]] <- lag_mods_biden_count[[i]]$cse
}
ses <- unlist(ses)

to_plot <- data.frame(
  day = rep(c(-1,0,1), each = 4),
  stop = rep(c("Biden", "Trump", "Harris", "Pence"),3),
  biden_count = c(coefs[1:4], 0.022, 0.015, 0.045, -0.003, coefs[5:8]),
  biden_se = c(ses[1:4], 0.026, 0.013, 0.015, 0.017, ses[5:8])
)

g2 <- ggplot(to_plot, aes(x = day, y = biden_count,
                          ymin = biden_count - 1.64*biden_se,
                          ymax = biden_count + 1.64*biden_se)) + 
  geom_point() + facet_wrap(~stop) + 
  geom_hline(yintercept = 0, lty = 2, col = "red") + 
  geom_errorbar(width = 0) +
  ggtitle("Effect on Individual Donors to Biden a Week Before and After Visit") +
  theme_minimal() + scale_x_continuous(breaks = c(-2,-1,0,1, 2),
                                       labels = c("",
                                                  "One Week Before",
                                                  "Actual Visit",
                                                  "One Week After",
                                                  ""),
                                       limits = c(-2,2)) +
  xlab("") + ylab("")

coefs <- unlist(lapply(lag_mods_trump_amount, coef))
ses <- as.list(rep(NA, 2))
for(i in 1:2){
  ses[[i]] <- lag_mods_trump_amount[[i]]$cse
}
ses <- unlist(ses)

to_plot <- data.frame(
  day = rep(c(-1,0,1), each = 4),
  stop = rep(c("Biden", "Trump", "Harris", "Pence"),3),
  trump_amount = c(coefs[1:4], 0.043, 0.134, 0.088, 0.087, coefs[5:8]),
  trump_se = c(ses[1:4], 0.020, 0.028, 0.059, 0.028, ses[5:8])
)

g3 <- ggplot(to_plot, aes(x = day, y = trump_amount,
                          ymin = trump_amount - 1.64*trump_se,
                          ymax = trump_amount + 1.64*trump_se)) + 
  geom_point() + facet_wrap(~stop) + 
  geom_hline(yintercept = 0, lty = 2, col = "red") + 
  geom_errorbar(width = 0) +
  ggtitle("Effect on Dollars Donated to Trump a Week Before and After Visit") +
  theme_minimal() + scale_x_continuous(breaks = c(-2,-1,0,1, 2),
                                       labels = c("",
                                                  "One Week Before",
                                                  "Actual Visit",
                                                  "One Week After",
                                                  ""),
                                       limits = c(-2,2)) +
  xlab("") + ylab("")

coefs <- unlist(lapply(lag_mods_trump_count, coef))
ses <- as.list(rep(NA, 2))
for(i in 1:2){
  ses[[i]] <- lag_mods_trump_count[[i]]$cse
}
ses <- unlist(ses)

to_plot <- data.frame(
  day = rep(c(-1,0,1), each = 4),
  stop = rep(c("Biden", "Trump", "Harris", "Pence"),3),
  trump_count = c(coefs[1:4], 0.008, 0.039, 0.040, 0.004, coefs[5:8]),
  trump_se = c(ses[1:4], 0.006, 0.008, 0.017, 0.008, ses[5:8])
)

g4 <- ggplot(to_plot, aes(x = day, y = trump_count,
                          ymin = trump_count - 1.64*trump_se,
                          ymax = trump_count + 1.64*trump_se)) + 
  geom_point() + facet_wrap(~stop) + 
  geom_hline(yintercept = 0, lty = 2, col = "red") + 
  geom_errorbar(width = 0) +
  ggtitle("Effect on Individual Donors to Trump a Week Before and After Visit") +
  theme_minimal() + scale_x_continuous(breaks = c(-2,-1,0,1, 2),
                                       labels = c("",
                                                  "One Week Before",
                                                  "Actual Visit",
                                                  "One Week After",
                                                  ""),
                                       limits = c(-2,2)) +
  xlab("") + ylab("")


grid.arrange(g1, g2, g3, g4)

stargazer(lag_mods_biden_amount[[1]], lag_mods_biden_count[[1]],
          lag_mods_trump_amount[[1]], lag_mods_trump_count[[1]],
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("FEs", "Zip \\& Day", "Zip \\& Day",
                             "Zip \\& Day", "Zip \\& Day"),
                           c("Counties", rep(1192,4)),
                           c("Days", rep(69,4))))

stargazer(lag_mods_biden_amount[[2]], lag_mods_biden_count[[2]],
          lag_mods_trump_amount[[2]], lag_mods_trump_count[[2]],
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("FEs", "Zip \\& Day", "Zip \\& Day",
                             "Zip \\& Day", "Zip \\& Day"),
                           c("Counties", rep(1192,4)),
                           c("Days", rep(69,4))))

#### Moving Average ####
contrib.dat <- contrib.dat %>% group_by(zip) %>% 
  mutate(biden_amount_under_500_avg = runMean(biden_amount_under_500, 5))
contrib.dat <- contrib.dat %>% group_by(zip) %>% 
  mutate(trump_amount_under_500_avg = runMean(trump_amount_under_500, 5))
contrib.dat <- contrib.dat %>% group_by(zip) %>% 
  mutate(biden_count_under_500_avg = runMean(biden_count_under_500, 5))
contrib.dat <- contrib.dat %>% group_by(zip) %>% 
  mutate(trump_count_under_500_avg = runMean(trump_count_under_500, 5))

mod_county_1_battleground <- felm(log(biden_amount_under_500_avg+1) ~
                                    biden_stop_county + trump_stop_county +
                                    harris_stop_county + pence_stop_county |
                                    zip + day | 0 | state_county, 
                                  data = contrib.dat[contrib.dat$state.abb %in%
                                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                                         "PA", "NH"),])
mod_county_2_battleground <- felm(log(biden_count_under_500_avg+1) ~
                                    biden_stop_county + trump_stop_county +
                                    harris_stop_county + pence_stop_county |
                                    zip + day | 0 | state_county, 
                                  data = contrib.dat[contrib.dat$state.abb %in%
                                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                                         "PA", "NH"),])
mod_county_3_battleground <- felm(log(trump_amount_under_500_avg+1) ~
                                    biden_stop_county + trump_stop_county +
                                    harris_stop_county + pence_stop_county |
                                    zip + day | 0 | state_county, 
                                  data = contrib.dat[contrib.dat$state.abb %in%
                                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                                         "PA", "NH"),])
mod_county_4_battleground <- felm(log(trump_count_under_500_avg+1) ~
                                    biden_stop_county + trump_stop_county +
                                    harris_stop_county + pence_stop_county |
                                    zip + day | 0 | state_county, 
                                  data = contrib.dat[contrib.dat$state.abb %in%
                                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                                         "PA", "NH"),])

stargazer(mod_county_1_battleground, mod_county_2_battleground,
          mod_county_3_battleground, mod_county_4_battleground,
          title = "County-level Results, 5-Day Moving Average, Battleground States (Donations under \\$500)",
          label = "county-level-moving-average",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("Counties", rep(1192,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables//county-level-moving-average.tex")

mod_county_1 <- felm(log(biden_amount_under_500_avg+1) ~
                                    biden_stop_county + trump_stop_county +
                                    harris_stop_county + pence_stop_county |
                                    zip + day | 0 | state_county, 
                                  data = contrib.dat)
mod_county_2 <- felm(log(biden_count_under_500_avg+1) ~
                                    biden_stop_county + trump_stop_county +
                                    harris_stop_county + pence_stop_county |
                                    zip + day | 0 | state_county, 
                                  data = contrib.dat)
mod_county_3<- felm(log(trump_amount_under_500_avg+1) ~
                                    biden_stop_county + trump_stop_county +
                                    harris_stop_county + pence_stop_county |
                                    zip + day | 0 | state_county, 
                                  data = contrib.dat)
mod_county_4 <- felm(log(trump_count_under_500_avg+1) ~
                                    biden_stop_county + trump_stop_county +
                                    harris_stop_county + pence_stop_county |
                                    zip + day | 0 | state_county, 
                                  data = contrib.dat)

stargazer(mod_county_1, mod_county_2,
          mod_county_3, mod_county_4,
          title = "County-level Results, 5-Day Moving Average, All States (Donations under \\$500)",
          label = "county-level-moving-average",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("Counties", rep(3204,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables//county-level-moving-average-all-states.tex")

mod_DMA_1_battleground <- felm(log(biden_amount_under_500_avg+1) ~
                                    biden_stop_DMA + trump_stop_DMA +
                                    harris_stop_DMA + pence_stop_DMA |
                                    zip + day | 0 | DMA, 
                                  data = contrib.dat[contrib.dat$state.abb %in%
                                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                                         "PA", "NH"),])
mod_DMA_2_battleground <- felm(log(biden_count_under_500_avg+1) ~
                                    biden_stop_DMA + trump_stop_DMA +
                                    harris_stop_DMA + pence_stop_DMA |
                                    zip + day | 0 | DMA, 
                                  data = contrib.dat[contrib.dat$state.abb %in%
                                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                                         "PA", "NH"),])
mod_DMA_3_battleground <- felm(log(trump_amount_under_500_avg+1) ~
                                    biden_stop_DMA + trump_stop_DMA +
                                    harris_stop_DMA + pence_stop_DMA |
                                    zip + day | 0 | DMA, 
                                  data = contrib.dat[contrib.dat$state.abb %in%
                                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                                         "PA", "NH"),])
mod_DMA_4_battleground <- felm(log(trump_count_under_500_avg+1) ~
                                    biden_stop_DMA + trump_stop_DMA +
                                    harris_stop_DMA + pence_stop_DMA |
                                    zip + day | 0 | DMA, 
                                  data = contrib.dat[contrib.dat$state.abb %in%
                                                       c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                         "WI", "MI", "OH", "NC", "GA", "FL",
                                                         "PA", "NH"),])

stargazer(mod_DMA_1_battleground, mod_DMA_2_battleground,
          mod_DMA_3_battleground, mod_DMA_4_battleground,
          title = "DMA-level Results, 5-Day Moving Average, Battleground States (Donations under \\$500)",
          label = "DMA-level-moving-average",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("Counties", rep(1192,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables//DMA-level-moving-average.tex")

mod_DMA_1 <- felm(log(biden_amount_under_500_avg+1) ~
                       biden_stop_DMA + trump_stop_DMA +
                       harris_stop_DMA + pence_stop_DMA |
                       zip + day | 0 | DMA, 
                     data = contrib.dat)
mod_DMA_2 <- felm(log(biden_count_under_500_avg+1) ~
                       biden_stop_DMA + trump_stop_DMA +
                       harris_stop_DMA + pence_stop_DMA |
                       zip + day | 0 | DMA, 
                     data = contrib.dat)
mod_DMA_3<- felm(log(trump_amount_under_500_avg+1) ~
                      biden_stop_DMA + trump_stop_DMA +
                      harris_stop_DMA + pence_stop_DMA |
                      zip + day | 0 | DMA, 
                    data = contrib.dat)
mod_DMA_4 <- felm(log(trump_count_under_500_avg+1) ~
                       biden_stop_DMA + trump_stop_DMA +
                       harris_stop_DMA + pence_stop_DMA |
                       zip + day | 0 | DMA, 
                     data = contrib.dat)

stargazer(mod_DMA_1, mod_DMA_2,
          mod_DMA_3, mod_DMA_4,
          title = "DMA-level Results, 5-Day Moving Average, All States (Donations under \\$500)",
          label = "DMA-level-moving-average",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("Counties", rep(3204,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables//DMA-level-moving-average-all-states.tex")



to_plot <- data.frame(
  coefficient = c(mod_DMA_1$coefficients, mod_DMA_2$coefficients,
                  mod_DMA_3$coefficients, mod_DMA_4$coefficients,
                  mod_DMA_1_battleground$coefficients, 
                  mod_DMA_2_battleground$coefficients, 
                  mod_DMA_3_battleground$coefficients, 
                  mod_DMA_4_battleground$coefficients),
  se = c(mod_DMA_1$cse, mod_DMA_2$cse,
         mod_DMA_3$cse, mod_DMA_4$cse,
         mod_DMA_1_battleground$cse, 
         mod_DMA_2_battleground$cse, 
         mod_DMA_3_battleground$cse, 
         mod_DMA_4_battleground$cse),
  visit = rep(c("Biden Stop", "Trump Stop", "Harris Stop", "Pence Stop"), 8),
  model = rep(c("Biden Dollar Amount", "Biden Count of Donations",
                "Trump Dollar Amount", "Trump Count of Donations"), each = 4, 2),
  position = rep(c(0.066667,.2,-.2,-.066667), each = 4, 2),
  sample = rep(c("All States", "Battleground States"), each = 16)
)

to_plot$visit <- factor(to_plot$visit, levels = c("Pence Stop", "Harris Stop",
                                                  "Trump Stop", "Biden Stop"))

pdf(file = "Figures//coefplot_moving_avg.pdf", width = 8, height = 4)
ggplot(to_plot, aes(x = visit, y = coefficient,
                    color = model, pch = model,
                    ymin = coefficient - 1.28*se,
                    ymax = coefficient + 1.28*se)) + 
  geom_errorbar(width = 0, position = position_nudge(x = to_plot$position)) +
  geom_point(position = position_nudge(x = to_plot$position), size = 2, fill = "white") + 
  coord_flip() + geom_hline(yintercept = 0, lty = 2, size = 1) +
  scale_shape_manual(name = "Outcome", values = c(21,24,16,17)) +
  scale_color_manual(name = "Outcome", values = c("Blue", "Blue", "Red", "Red")) +
  theme_bw() + ylim(c(-.1, .1)) + xlab("") + ylab("Esimated Coefficient") +
  facet_wrap(~ sample) + theme(panel.grid.minor = element_blank())
dev.off()

biden_treated_counties <- aggregate(biden_stop_DMA ~ DMA, contrib.dat, sum)
biden_treated_counties <- subset(biden_treated_counties, biden_stop_DMA>0)
biden_amt_mean <- aggregate(biden_amount_under_500 ~ DMA, contrib.dat, sum)
biden_amt_mean <- mean(biden_amt_mean$biden_amount_under_500[biden_amt_mean$DMA %in% biden_treated_counties$DMA])

biden_count_mean <- aggregate(biden_count_under_500 ~ DMA, contrib.dat, sum)
biden_count_mean <- mean(biden_count_mean$biden_count_under_500[biden_count_mean$DMA %in% biden_treated_counties$DMA])

trump_treated_counties <- aggregate(trump_stop_DMA ~ DMA, contrib.dat, sum)
trump_treated_counties <- subset(trump_treated_counties, trump_stop_DMA>0)
trump_amt_mean <- aggregate(trump_amount_under_500 ~ DMA, contrib.dat, sum)
trump_amt_mean <- mean(trump_amt_mean$trump_amount_under_500[trump_amt_mean$DMA %in% trump_treated_counties$DMA])

trump_count_mean <- aggregate(trump_count_under_500 ~ DMA, contrib.dat, sum)
trump_count_mean <- mean(trump_count_mean$trump_count_under_500[trump_count_mean$DMA %in% trump_treated_counties$DMA])


means <- data.frame(visit = rep(c("Biden Stop", "Trump Stop",
                                  "Harris Stop", "Pence Stop"), 8),
                    treatment_mean = rep(c(biden_amt_mean, biden_count_mean,
                                           trump_amt_mean, trump_count_mean), each = 4, 2))


to_plot$treatment_mean <- means$treatment_mean/69
to_plot$control_mean <- to_plot$treatment_mean -
  to_plot$treatment_mean*(exp(to_plot$coefficient)-1)
to_plot$change <- to_plot$treatment_mean - to_plot$control_mean


#### Over 500 ####

contrib.dat <- contrib.dat %>% group_by(zip) %>% 
  mutate(biden_amount_over_500_avg = runMean(biden_amount_over_500, 5))
contrib.dat <- contrib.dat %>% group_by(zip) %>% 
  mutate(trump_amount_over_500_avg = runMean(trump_amount_over_500, 5))
contrib.dat <- contrib.dat %>% group_by(zip) %>% 
  mutate(biden_count_over_500_avg = runMean(biden_count_over_500, 5))
contrib.dat <- contrib.dat %>% group_by(zip) %>% 
  mutate(trump_count_over_500_avg = runMean(trump_count_over_500, 5))

mod_DMA_1_battleground <- felm(log(biden_amount_over_500_avg+1) ~
                                 biden_stop_DMA + trump_stop_DMA +
                                 harris_stop_DMA + pence_stop_DMA |
                                 zip + day | 0 | DMA, 
                               data = contrib.dat[contrib.dat$state.abb %in%
                                                    c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                      "WI", "MI", "OH", "NC", "GA", "FL",
                                                      "PA", "NH"),])
mod_DMA_2_battleground <- felm(log(biden_count_over_500_avg+1) ~
                                 biden_stop_DMA + trump_stop_DMA +
                                 harris_stop_DMA + pence_stop_DMA |
                                 zip + day | 0 | DMA, 
                               data = contrib.dat[contrib.dat$state.abb %in%
                                                    c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                      "WI", "MI", "OH", "NC", "GA", "FL",
                                                      "PA", "NH"),])
mod_DMA_3_battleground <- felm(log(trump_amount_over_500_avg+1) ~
                                 biden_stop_DMA + trump_stop_DMA +
                                 harris_stop_DMA + pence_stop_DMA |
                                 zip + day | 0 | DMA, 
                               data = contrib.dat[contrib.dat$state.abb %in%
                                                    c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                      "WI", "MI", "OH", "NC", "GA", "FL",
                                                      "PA", "NH"),])
mod_DMA_4_battleground <- felm(log(trump_count_over_500_avg+1) ~
                                 biden_stop_DMA + trump_stop_DMA +
                                 harris_stop_DMA + pence_stop_DMA |
                                 zip + day | 0 | DMA, 
                               data = contrib.dat[contrib.dat$state.abb %in%
                                                    c("NV", "AZ", "CO", "TX", "MN", "IA",
                                                      "WI", "MI", "OH", "NC", "GA", "FL",
                                                      "PA", "NH"),])

stargazer(mod_DMA_1_battleground, mod_DMA_2_battleground,
          mod_DMA_3_battleground, mod_DMA_4_battleground,
          title = "DMA-level Results, 5-Day Moving Average, Battleground States (Donations over \\$500)",
          label = "DMA-level-moving-average",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("Counties", rep(1192,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables//DMA-level-moving-average-over500.tex")

mod_DMA_1 <- felm(log(biden_amount_over_500_avg+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, 
                  data = contrib.dat)
mod_DMA_2 <- felm(log(biden_count_over_500_avg+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, 
                  data = contrib.dat)
mod_DMA_3<- felm(log(trump_amount_over_500_avg+1) ~
                   biden_stop_DMA + trump_stop_DMA +
                   harris_stop_DMA + pence_stop_DMA |
                   zip + day | 0 | DMA, 
                 data = contrib.dat)
mod_DMA_4 <- felm(log(trump_count_over_500_avg+1) ~
                    biden_stop_DMA + trump_stop_DMA +
                    harris_stop_DMA + pence_stop_DMA |
                    zip + day | 0 | DMA, 
                  data = contrib.dat)

stargazer(mod_DMA_1, mod_DMA_2,
          mod_DMA_3, mod_DMA_4,
          title = "DMA-level Results, 5-Day Moving Average, All States (Donations over \\$500)",
          label = "DMA-level-moving-average",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("Counties", rep(3204,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables//DMA-level-moving-average-all-states-over500.tex")


#### Interactions ####
mod_DMA_1 <- felm(log(biden_amount_under_500_avg+1) ~
                       biden_stop_DMA + biden_stop_DMA:trump.margin +
                       trump_stop_DMA + trump_stop_DMA:trump.margin +
                       harris_stop_DMA + harris_stop_DMA:trump.margin +
                       pence_stop_DMA + pence_stop_DMA:trump.margin|
                       zip + day | 0 | DMA, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_DMA_2 <- felm(log(biden_count_under_500_avg+1) ~
                       biden_stop_DMA + biden_stop_DMA:trump.margin +
                       trump_stop_DMA + trump_stop_DMA:trump.margin +
                       harris_stop_DMA + harris_stop_DMA:trump.margin +
                       pence_stop_DMA + pence_stop_DMA:trump.margin |
                       zip + day | 0 | DMA, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_DMA_3 <- felm(log(trump_amount_under_500_avg+1) ~
                       biden_stop_DMA + biden_stop_DMA:trump.margin +
                       trump_stop_DMA + trump_stop_DMA:trump.margin +
                       harris_stop_DMA + harris_stop_DMA:trump.margin +
                       pence_stop_DMA + pence_stop_DMA:trump.margin|
                       zip + day | 0 | DMA, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
mod_DMA_4 <- felm(log(trump_count_under_500_avg+1) ~
                       biden_stop_DMA + biden_stop_DMA:trump.margin +
                       trump_stop_DMA + trump_stop_DMA:trump.margin +
                       harris_stop_DMA + harris_stop_DMA:trump.margin +
                       pence_stop_DMA + pence_stop_DMA:trump.margin |
                       zip + day | 0 | DMA, 
                     data = contrib.dat[contrib.dat$state.abb %in%
                                          c("NV", "AZ", "CO", "TX", "MN", "IA",
                                            "WI", "MI", "OH", "NC", "GA", "FL",
                                            "PA", "NH"),])
stargazer(mod_DMA_1, mod_DMA_2,
          mod_DMA_3, mod_DMA_4,
          title = "DMA-level Results, 5-Day Moving Average, Conditional Effects, Battleground States (Donations under \\$500)",
          label = "DMA-level-moving-average-int",
          font.size = "scriptsize",
          keep.stat = c("n"),
          dep.var.labels = c("Biden", "Biden", "Trump", "Trump"),
          multicolumn = T,
          column.labels = c("Dollar Amount", "Count",
                            "Dollar Amount", "Count"),
          covariate.labels = c("Biden Stop", "Trump Stop",
                               "Harris Stop", "Pence Stop"),
          add.lines = list(c("Counties", rep(107,4)),
                           c("Days", rep(69,4))),
          apply.p = function(x){x/2},
          out = "Tables//DMA-level-moving-average-battleground-int.tex")
#### Trump Margin in Stops ####
trump_marg <- read.csv("trump_margin.csv", stringsAsFactors = F)
trump_marg$state_county <- gsub("\\.|\\-", "", tolower(trump_marg$state_county))

stops$state_county <- tolower(paste(stops$state.abb, gsub("\\.|\\-", "",stops$county)))

t.test(trump_marg$trump.margin[trump_marg$state_county %in% 
                               stops$state_county[stops$candidate=="Donald Trump"]],
        trump_marg$trump.margin[!(trump_marg$state_county %in% 
                               stops$state_county[stops$candidate=="Donald Trump"])])
t.test(trump_marg$trump.margin[trump_marg$state_county %in% 
                                 stops$state_county[stops$candidate=="Joe Biden"]],
       trump_marg$trump.margin[!(trump_marg$state_county %in% 
                                   stops$state_county[stops$candidate=="Joe Biden"])])
t.test(trump_marg$trump.margin[trump_marg$state_county %in% 
                                 stops$state_county[stops$candidate=="Mike Pence"]],
       trump_marg$trump.margin[!(trump_marg$state_county %in% 
                                   stops$state_county[stops$candidate=="Mike Pence"])])
t.test(trump_marg$trump.margin[trump_marg$state_county %in% 
                                 stops$state_county[stops$candidate=="Kamala Harris"]],
       trump_marg$trump.margin[!(trump_marg$state_county %in% 
                                   stops$state_county[stops$candidate=="Kamala Harris"])])

